
package generalapi;

import java.io.IOException;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.StringJoiner;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import static java.util.stream.IntStream.range;

/**
 *
 * @author Administrator
 */
public class GeneralAPI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        
        Logger logger = Logger.getLogger(GeneralAPI.class.getName());
        logger.setLevel(Level.FINE);
        for (Handler handler : logger.getParent().getHandlers()) {
            handler.setLevel(Level.FINE);
        }
        logger.log(Level.WARNING, "warning信息");
        logger.log(Level.INFO, "info信息");
        logger.log(Level.CONFIG, "config信息");
        logger.log(Level.FINE, "fine 信息");
        
        System.out.println(logger.getLevel());
        System.out.println(logger.getParent().getLevel());
        
        Logger global = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
        System.out.println(global.getParent().getLevel());
        
        System.out.println(logger.getHandlers().length);//显示0，表示没有Handler
        
        for (Handler handler : logger.getParent().getHandlers()) {
            System.out.println(handler);
            System.out.println(handler.getLevel());
        }
        
        logger.setLevel(Level.CONFIG);
        FileHandler handler = new FileHandler("%h/config.log");
        handler.setLevel(Level.CONFIG);
        logger.addHandler(handler);
        logger.config("Logger组态完成");//直接以Level.CONFIG层级输出信息
        
        ConsoleHandler ch = new ConsoleHandler();
        ch.setLevel(Level.CONFIG);
        ch.setFormatter(new Formatter() {
            @Override
            public String format(LogRecord record){
                return "日志来自" + record.getSourceClassName() + record.getSourceMethodName() + "\n" 
                        + "\t层级\t"  + record.getLevel() + "\n\t信息\t" + record.getMessage() 
                        + "\n\t时间" + new Date(record.getMillis()) + "\n";
            }
        });
        logger.addHandler(ch);
        logger.config("自定义 Formatter 信息 ");
        
        
        ResourceBundle res = ResourceBundle.getBundle("generalapi.msg");//类路径
        System.out.println(res.getString("key"));
        System.out.println(res.getString("value"));
        
        for (String arg : "zhangsan,lisi,wangwu".split(",")) {
            System.out.println(arg);
        }
        
        for (String arg : "zhangsanorzlisiorzwangwu".split("orz")) {
            System.out.println(arg);
        }
        
        String[] regex = {".*foo", ".*?foo", ".*+foo"};
        for (String string : regex) {
            System.out.println("xfooxxxxxxfoo".replaceAll(string, "Orz"));
        }
        
        for (String s : "abc dog def doggie hij".split("\\bdog\\b")) {//边界比较，\b为单词边界
            System.out.println(s.trim());
        }
        
        StringJoiner joiner = new StringJoiner("-");
        String msg = joiner.add("zhang").add("san").add("ok").toString();
        System.out.println(msg);
        
        range(0, 100).forEach(System.out :: println);
        
        IntStream charS = "zhangsan".chars();
        IntStream codeS = "zhangsan".codePoints();
        System.out.println(charS + "\n" + codeS);
        
        Random random = new Random();
        DoubleStream doubleStream = random.doubles();
        IntStream intStream = random.ints(0, 100);
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("找不到指定的类");
        }
    }
    
}
